package stage
{
	import control.Arrowing;
	import control.MonsterControl;
	
	import data.DataBase;
	import data.Tower;
	
	import events.DamageToMonster;
	import events.FarmClickEvent;
	import events.GameEvent;
	import events.MonsterDieEvent;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import manager.TdManage;
	
	import utils.Image;
	import utils.XMLHelper;
	
	import view.Arrow;
	import view.BuildBar;
	import view.BuildSelect;
	import view.Building;
	import view.Farm;
	import view.Map;
	import view.TowerView;
	import view.UpgradeBar;
	
	public class Stage_lv1 extends Sprite
	{
		
		public function Stage_lv1()
		{
			
			//加载xml
			XMLHelper.loadXML("monster4.xml",onloaded)
			
			//加载数据类
			dat = new DataBase();
			
			//设置路线
			lineArr = dat.line_arr;
			
			//添加地图 背景
			map = new Map();
			addChild(map);
			map.url = dat.bg_url;
			
			//添加空地
			buildSelect = new BuildSelect();
			addChild(buildSelect);
			
			mainHptext = new TextField();
			mainHptext.text = mainHp.toString();
			addChild(mainHptext);
			
			moneyText = new TextField();
			moneyText.text = money.toString();
			addChild(moneyText);
			
			addChild(buildBar);
			buildBar.visible = false;
			
			moneyText.x = 50
			//监听空地点击事件
//			addEventListener(FarmClickEvent.FARM_CLICKED,farmClicked);
			buildSelect.addEventListener(GameEvent.BUILDBAR_VIEW,buildBarViewHandle);
			//监听 箭塔攻击怪物事件
			addEventListener(DamageToMonster.DAMAGE_TO_MONSTER,damageToMonster);
			//监听怪物死亡事件
			addEventListener(MonsterDieEvent.MONSTER_DIE_EVENT,removeMonster);
			
			init();
			
		}
		
		/**
		 * 塔的初始化-inits 
		 */		
		private function init():void{
			map.addEventListener(MouseEvent.CLICK,bgClickHandle);
			//建筑物被单击
			addEventListener(GameEvent.UPGRADEBAR_SHOW,upgradeBarHandle);
			box = new Sprite();
			addChild(box);
			
			
			
			towerRange = new Sprite();
			addChild(towerRange);
			//			setChildIndex(towerRange,numChildren-1);
			
			buildSelect = new BuildSelect();
			addChild(buildSelect);
			
			manage = TdManage.getInstance();
			addChild(manage);
			
			buildBar = BuildBar.getInstence();
			addChild(buildBar);
			buildBar.visible = false;
			
			manage.addEventListener(TimerEvent.TIMER, onTimer);
			
			//侦听建筑物选择器上的建筑物类型的点击事件,此事件触发说明有塔要被建立
			buildBar.addEventListener(GameEvent.CREAT_BUILD_VIEW,creatBuildViewHandle);
			
			buildBar.addEventListener(GameEvent.ATTACK_RANGE,attackRangeHandle);
			buildBar.addEventListener(GameEvent.CLEAR_TOWERRANGE,clearRangeHandle);
			
			buildSelect.addEventListener(GameEvent.BUILDBAR_VIEW,buildBarViewHandle);
		}
		/**
		 *金币显示文本 
		 */		
		public static var moneyText:TextField;
		/**
		 *金币 
		 */		
		public static var money:uint=500;
		/**
		 *关卡总血量显示文本 
		 */		
		public static var mainHptext:TextField
		/**
		 *管卡总血量，允许怪物到终点的数量 
		 */		
		public static var mainHp:uint=20;
		/**
		 *塔 
		 */		
		private var building:Building;
		/**
		 *数据 
		 */		
		public var dat:DataBase;
		/**
		 *背景地图 
		 */		
		private var map:Map;
		/**
		 *空地 
		 */		
		private var buildSelect:BuildSelect;
		/**
		 *塔数组 
		 */		
		public var buildingArr:Array = [];
		/**
		 * 怪物数组
		 */		
		public var monsterArr:Array = [];
		/**
		 * 怪物数据
		 */		
		private var xml:XMLList;
		
		private var lineArr:Array;
		/**
		 *删除怪物
		 */		
		protected function removeMonster(event:MonsterDieEvent):void
		{
			var monster:MonsterControl = event.monster as MonsterControl;
			removeChild(monster);
			monster = null
		}
		/**
		 *塔发射的伤害传给怪物 
		 */		
		protected function damageToMonster(event:DamageToMonster):void
		{
			
			var monster:MonsterControl = event.monster as MonsterControl;
			monster.damage=event.damage;
		}
		/**
		 *加载完毕xml 后添加怪物
		 */		
		private function onloaded(data:Object):void
		{
			xml = new XMLList(data);
			
			//添加怪物
			var timer:Timer = new Timer(3000,10);
			timer.addEventListener(TimerEvent.TIMER,addMonster);
			timer.start();
			addMonster(null);
		}		
		
		/**
		 *添加怪物 
		 */		
		protected function addMonster(event:TimerEvent):void
		{
			var monster:MonsterControl = new MonsterControl(xml,monsterArr,lineArr);
			addChild(monster)
			monsterArr.push(monster);
		}		
		
		/**
		 *空地点击事件
		 * 添加箭塔
		 * 添加箭头
		 */		
		protected function farmClicked(event:FarmClickEvent):void
		{
			
			/*var farm:Farm = event.farm
			addBuilding(farm);*/
			
			
			
		}
		/**
		 *添加箭头 
		 * @param farm:Point
		 * 
		 */		
		public function addArrow(farm:Point):Arrow
		{
			var arrow:Arrow = new Arrow();
			//addChildAt(arrow,1)
			var arrowing:Arrowing = new Arrowing(arrow,farm,monsterArr);
			return arrow;
		}
		/**
		 *添加箭塔 
		 * @param farm:Point
		 * 
		 */		
		private function addBuilding(farm:Farm):void
		{
			if(money>=300)
			{
				updateMoney(-300)
				
				building = new Building(dat.building_url);
				addChild(building);
				//设置空地坐标
				building.x = farm.x;
				building.y = farm.y;
				
				
				var point:Point = new Point(farm.x,farm.y);
				addArrow(point);
				
				removeChild(farm);
				farm = null;
				
			}
		}
		public static function updateMainHp(_hp:int):void
		{
			mainHp+=_hp
			mainHptext.text = mainHp.toString();
		}
		public static function updateMoney(_money:int):void
		{
			money+=_money
			moneyText.text = money.toString();
		}
		
		
		
		/////////////////////
		/**
		 * 塔的升级菜单 
		 */	
		private var upgradeBar:UpgradeBar = UpgradeBar.getInstence();
		/**
		 * 声明建筑物选择器 
		 */		
		public var buildBar:BuildBar = BuildBar.getInstence();
		/**
		 * 显示塔的建筑物选择器 
		 * @param event
		 * 
		 */		
		private function buildBarViewHandle(event:GameEvent):void{
//			trace(event);
			var img:Image = event.obj as Image;
			buildBar.x = img.x + 25;
			buildBar.y = img.y + 20;
			setChildIndex(buildBar,numChildren-1);
			buildBar.orShow();
			if(!buildBar.visible){
				buildBar.orShow();
			}
			if(upgradeBar.visible){
				upgradeBar.orShow();
			}
		}
		
		
		
		/**
		 * 声明数据控制对象 
		 */		
		private var manage:TdManage;
		
		private var box:Sprite = new Sprite();
		
		private var towerRange:Sprite;
		
		
		/**
		 * 清除塔的攻击范围显示 
		 * @param event
		 * 
		 */		
		private function clearRangeHandle(event:Event):void{
			if(towerRange.visible){
				//				removeChild(towerRange);
				towerRange.visible = false;
			}
		}
		
		/**
		 * 显示塔的攻击范围 
		 * @param event
		 * 
		 */		
		private function attackRangeHandle(event:GameEvent):void{
			var obj:Object = event.obj;
			drawRange(obj);
			towerRange.visible = true;
		}
		
		private function drawRange(_obj:Object):void{
			var point:Point = _obj.p;
			var r:Number = _obj.r;
			towerRange.graphics.clear();
			towerRange.graphics.lineStyle(1,0x879F2F);
			towerRange.graphics.beginFill(0x879F2F,.2);
			towerRange.graphics.drawCircle(point.x,point.y,r);
			towerRange.graphics.endFill();
			
		}
		
		private function upgradeBarHandle(event:GameEvent):void{
			var mc:MovieClip = event.obj as MovieClip;
			
			upgradeBar.x = mc.x;
			upgradeBar.y = mc.y;
			addChild(upgradeBar);
			setChildIndex(upgradeBar,numChildren-1);
			if(!upgradeBar.visible){
				upgradeBar.orShow();
			}
		}
		
		/**
		 * 当地图图片被单击时 
		 * @param event
		 * 
		 */		
		private function bgClickHandle(event:MouseEvent):void{
			buildBar.setHid();
			if(upgradeBar.visible){
				upgradeBar.orShow();
			}
		}		
		
		
		protected function onTimer(event:Event):void{
			//dosomething;
		}
		
		/**
		 * 显示塔的建筑物选择器 
		 * @param event
		 * 
		 */		
		/*private function buildBarViewHandle(event:GameEvent):void{
			var img:Image = event.obj as Image;
			buildBar.x = img.x + 25;
			buildBar.y = img.y + 20;
			setChildIndex(buildBar,numChildren-1);
			buildBar.orShow();
			if(!buildBar.visible){
				buildBar.orShow();
			}
			if(upgradeBar.visible){
				upgradeBar.orShow();
			}
		}*/
		/**
		 * 创建所要显示的塔 
		 * @param event
		 * 
		 */		
		protected function creatBuildViewHandle(event:GameEvent):void{
			if(money>=300)
			{
				updateMoney(-300)
				
			buildBar.orShow();
			var t:Tower = event.obj as Tower;
			var point:Point = new Point(t.px,t.py);
			var towerView:TowerView = new TowerView(t,addArrow(point));
			addChild(towerView);
			
			
			}
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}